package db

import (
	"gitee.com/golang-lib/go-framework/initialize"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

var Mdb *gorm.DB

func InitDB() {
	conf := initialize.ConfigInstance.DB
	_db, err := gorm.Open(mysql.Open(conf.Url), &gorm.Config{
		Logger: logger.New(log.New(os.Stdout, "", log.LstdFlags), logger.Config{
			SlowThreshold:             200 * time.Millisecond,
			LogLevel:                  logger.Info,
			IgnoreRecordNotFoundError: false,
			Colorful:                  true,
		}),
	})

	if err != nil {
		log.Fatalln("mysql init error: ", err)
	}

	if initialize.IsProd() {
		Mdb = _db
	} else {
		Mdb = _db.Debug()
	}
}
